package cn.com.homeyoda.aimate.aihealth.ui.health_qa;

import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.SUBMIT_ANSWER;

import android.app.Application;

import androidx.annotation.NonNull;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.AnswerAnalysisBean;
import cn.com.homeyoda.aimate.aihealth.bean.AnswerBean;
import cn.com.homeyoda.aimate.aihealth.bean.BaseResponse2;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTypeListBean;
import cn.com.homeyoda.aimate.aihealth.bean.QuestionListBean;
import cn.com.homeyoda.aimate.aihealth.bean.QuizListBean;
import cn.com.homeyoda.aimate.aihealth.bean.UserRecordsBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class HealthQaViewModel extends BaseViewModel<Repository> {

    public StateLiveData<List<MessageTypeListBean.DataBean>> typeListData;
    public StateLiveData<List<QuizListBean.RowsBean>> qaListData;

    public StateLiveData<List<QuestionListBean.DataBean>> questionTopicData;

    public StateLiveData<List<UserRecordsBean.RowsBean>> recodeListData;

    public StateLiveData<AnswerAnalysisBean.DataBean> answerAnalysisData;

    public String htmlContent = "<!DOCTYPE html>" +
            "<html lang=\"zh\">" +
            "<head>" +
            "  <meta charset=\"UTF-8\">" +
            "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" +
            "  <title>教育文章</title>" +
            "  <style>" +
            "    body {" +
            "      background-color: transparent;" +
            "      margin: 0;" +
            "      font-family: \"Helvetica Neue\", Arial, sans-serif;" +
            "      color: #000;" +
            "      line-height: 1.7;" +
            "    }" +
            "    .content {" +
            "      max-width: 780px;" +
            "      margin: auto;" +
            "      margin-top: 36px;" +
            "    }" +
            "    img {" +
            "      width: 100%;" +
            "      height: auto;" +
            "      margin: 16px 0;" +
            "    }" +
            "    p {" +
            "      margin-bottom: 1em;" +
            "      text-indent: 2em;" +
            "    }" +
            "  </style>" +
            "</head>" +
            "<body>" +
            "  <div class=\"content\">" +
            "    <p>MBIT 人格理论基于荣格心理测试加以研发而成的，帮助解释为什么不同的人对不同事物感兴趣，为什么有些人不能相互理解等。不少小伙伴运用MBIT选择就业方向，还有公司利用这个测试作为参考应用于团队沟通、组织建设、提高效率等。</p>" +
            "    <p> MBTI旨在帮助人们了解自我的本来面目，即个人与生俱来的性格。在众多分析性格的理论模型中，MBTI科学、有效、应用面广，很受欢迎。</p>" +
            "    <p>MBTI的理论基础基于瑞士著名心理学家卡尔·荣格的心理学类型理论，由美国一对母女——凯瑟琳·布里格思和她的女儿伊莎贝尔·布里格斯·迈尔斯进一步发展并研发而成。</p>" +
            "<div style=\"overflow: hidden; margin: 16px 0;\">" +
            "  <img src=\"file:///android_res/mipmap/ic_image_health_qa.png\"" +
            "       style=\"float: left; width: 30%; margin-right: 16px; margin-bottom: 4px;\">" +
            "  <p style=\"text-indent: 2em;\">经近80年的实践和发展，MBTI已成为世界通用的性格测试，被广泛应用于自我了解与发展、职业规划、专业选择、团队建设管理和领导培训、问题解决、人际关系辅导、教育及课程开发、多元化和多文化培训等诸多领域。</p>" +
            "  <p style=\"text-indent: 2em;\">MBTI用四个维度构成了人们的整个心理活动过程。性格的每个维度都有彼此对立的极端，在一个维度上到底是哪种取向取决于个人的偏好，这样共有八种性格偏好，每种用一个字母来表示。</p>" +
            "</div>" +
            "</body>" +
            "</html>";

    public HealthQaViewModel(@NonNull Application application, Repository model) {
        super(application, model);
        typeListData = new StateLiveData<>();
        typeListData.setValue(new ArrayList<>());

        qaListData = new StateLiveData<>();
        qaListData.setValue(new ArrayList<>());

        questionTopicData = new StateLiveData<>();
        questionTopicData.setValue(new ArrayList<>());

        recodeListData = new StateLiveData<>();
        recodeListData.setValue(new ArrayList<>());

        answerAnalysisData = new StateLiveData<>();

    }


    /**
     * 获取问答类别
     * @param isList true 问答列表 false 问答记录
     */
    public void getTypeList(boolean isList) {
        model.getQuizTypeList()
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(HealthQaViewModel.this)
                .subscribe(new Observer<MessageTypeListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        typeListData.postLoading();
                    }

                    @Override
                    public void onNext(MessageTypeListBean response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            MessageTypeListBean.DataBean dataBean = new MessageTypeListBean.DataBean();
                            dataBean.setTypeName("全部");
                            dataBean.setTypeValue("");
                            response.getData().add(0, dataBean);
                            typeListData.postValueAndSuccess(response.getData());
                            if (isList) {
                                getQaListData("");
                            } else {
                                getRecodeList("");
                            }
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        typeListData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void getQaListData(String type) {
        model.getQuizList(type, "20", "1")
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(HealthQaViewModel.this)
                .subscribe(new Observer<QuizListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        qaListData.postLoading();
                    }

                    @Override
                    public void onNext(QuizListBean response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            qaListData.postValueAndSuccess(response.getRows());
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        qaListData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void getHealthQaData(String id) {
        model.getQuestionList(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(HealthQaViewModel.this)
                .subscribe(new Observer<QuestionListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        questionTopicData.postLoading();
                    }

                    @Override
                    public void onNext(QuestionListBean response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            questionTopicData.postValueAndSuccess(response.getData());
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        questionTopicData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void getRecodeList(String quizType) {
        model.getUserRecords(model.getUserId(), quizType,"20", "1")
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(HealthQaViewModel.this)
                .subscribe(new Observer<UserRecordsBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        recodeListData.postLoading();
                    }

                    @Override
                    public void onNext(UserRecordsBean response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            recodeListData.postValueAndSuccess(response.getRows());
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        recodeListData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void submitAnswer(AnswerBean answer) {
        answer.setUserId(model.getUserId());
        model.submitAnswer(answer)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(HealthQaViewModel.this)
                .subscribe(new Observer<BaseResponse2>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResponse2 response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            LiveEventBus.get(SUBMIT_ANSWER).post(response.getMsg());
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void getAnswerAnalysis(String quizRecordId) {
        // quizRecordId = "1351567018180349952";
        model.getAnswerAnalysis(quizRecordId)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(HealthQaViewModel.this)
                .subscribe(new Observer<AnswerAnalysisBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        answerAnalysisData.postLoading();
                    }

                    @Override
                    public void onNext(AnswerAnalysisBean response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            answerAnalysisData.postValueAndSuccess(response.getData());
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        answerAnalysisData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

}
